package com.htgd.gardenexpo.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.htgd.gardenexpo.entity.GeConGisresource;
import com.htgd.gardenexpo.dao.GeConGisresourceDao;
import com.htgd.gardenexpo.entity.PageQuery;
import com.htgd.gardenexpo.service.GeConGisresourceService;
import com.htgd.gardenexpo.vo.ParkVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.htgd.gardenexpo.utils.RestTemplateUtils.sendGetRequest;

/**
 * (GeConGisresource)表服务实现类
 *
 * @author makejava
 * @since 2021-07-20 14:18:00
 */
@Service("geConGisresourceService")
public class GeConGisresourceServiceImpl implements GeConGisresourceService {
    @Resource
    private GeConGisresourceDao geConGisresourceDao;

    @Value("${zg.park_url}")
    private String parkUrl;

    /**
     * 通过ID查询单条数据
     *
     * @param gisId 主键
     * @return 实例对象
     */
    @Override
    public GeConGisresource queryById(Integer gisId) {
        GeConGisresource geConGisresource = geConGisresourceDao.queryById(gisId);
        //获取综管停车数
        JSONObject jsonObject = sendGetRequest(parkUrl);
        Integer code = jsonObject.getInteger("code");
        if(200==code){
            JSONArray data = jsonObject.getJSONArray("data");
            // 转换为 List
            List<ParkVO> list = new ArrayList<>();
            for (int i = 0; i < data.size(); i++) {
                list.add(data.getJSONObject(i).toJavaObject(ParkVO.class));
            }
            if(212==geConGisresource.getGisId()){//北停车chang
                ParkVO parkVO1 = new ParkVO();
                for (ParkVO parkVO : list) {
                    if("20250120120407975".equals(parkVO.getParkCode())){
                        parkVO1 =  parkVO;
                    }
                }
                geConGisresource.setTotalLot(parkVO1.getTotalLot());
                geConGisresource.setLeftLot(parkVO1.getLeftLot());
                geConGisresource.setIntroduction("空余车位/总车位："+parkVO1.getLeftTempLot()+"/"+parkVO1.getTotalLot());
            }
            if(213==geConGisresource.getGisId()){//北停车chang
                ParkVO parkVO1 = new ParkVO();
                for (ParkVO parkVO : list) {
                    if("20240604161843464".equals(parkVO.getParkCode())){
                        parkVO1 =  parkVO;
                    }
                }
                geConGisresource.setTotalLot(parkVO1.getTotalLot());
                geConGisresource.setLeftLot(parkVO1.getLeftLot());
                geConGisresource.setIntroduction("空余车位/总车位："+parkVO1.getLeftTempLot()+"/"+parkVO1.getTotalLot());
            }
        }
        return geConGisresource;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<GeConGisresource> queryAllByLimit(int offset, int limit) {
        return this.geConGisresourceDao.queryAllByLimit(offset, limit);
    }

    @Override
    public List<GeConGisresource> queryByTypeId(int gisTypeId) {
        return this.geConGisresourceDao.queryByTypeId(gisTypeId);
    }
    @Override
    public List<GeConGisresource> queryByEId(int eId) {
        return this.geConGisresourceDao.queryByEId(eId);
    }

    @Override
    public List<GeConGisresource> queryByName(String name) {
        return this.geConGisresourceDao.queryByName(name);
    }

    @Override
    public List<GeConGisresource> queryAllByValue(PageQuery pageQuery) {
        return this.geConGisresourceDao.queryAllByValue(pageQuery);
    }
    @Override
    public List<GeConGisresource> queryAllByValueNoContent(PageQuery pageQuery) {
        return this.geConGisresourceDao.queryAllByValueNoContent(pageQuery);
    }

    /**
     * 新增数据
     *
     * @param geConGisresource 实例对象
     * @return 实例对象
     */
    @Override
    public GeConGisresource insert(GeConGisresource geConGisresource) {
        this.geConGisresourceDao.insert(geConGisresource);
        return geConGisresource;
    }

    /**
     * 修改数据
     *
     * @param geConGisresource 实例对象
     * @return 实例对象
     */
    @Override
    public GeConGisresource update(GeConGisresource geConGisresource) {
        this.geConGisresourceDao.update(geConGisresource);
        return this.queryById(geConGisresource.getGisId());
    }

    /**
     * 通过主键删除数据
     *
     * @param gisId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer gisId) {
        return this.geConGisresourceDao.deleteById(gisId) > 0;
    }

    @Override
    public List<GeConGisresource> list2(Integer type, Integer zoom) {
        List<GeConGisresource> geConGisresources = geConGisresourceDao.list2(type, zoom);
        if(44==type){//停车场
            //获取综管停车数
            JSONObject jsonObject = sendGetRequest(parkUrl);
            Integer code = jsonObject.getInteger("code");
            if(200==code){
                JSONArray data = jsonObject.getJSONArray("data");
                // 转换为 List
                List<ParkVO> list = new ArrayList<>();
                for (int i = 0; i < data.size(); i++) {
                    list.add(data.getJSONObject(i).toJavaObject(ParkVO.class));
                }
                //循环遍历停车场
                for (GeConGisresource geConGisresource : geConGisresources) {
                    if(212==geConGisresource.getGisId()){//北停车chang
                        ParkVO parkVO1 = new ParkVO();
                        for (ParkVO parkVO : list) {
                            if("20250120120407975".equals(parkVO.getParkCode())){
                                parkVO1 =  parkVO;
                            }
                        }
                        geConGisresource.setTotalLot(parkVO1.getTotalLot());
                        geConGisresource.setLeftLot(parkVO1.getLeftLot());
                        geConGisresource.setIntroduction("空余车位/总车位："+parkVO1.getLeftTempLot()+"/"+parkVO1.getTotalLot());
                    }
                    if(213==geConGisresource.getGisId()){//北停车chang
                        ParkVO parkVO1 = new ParkVO();
                        for (ParkVO parkVO : list) {
                            if("20240604161843464".equals(parkVO.getParkCode())){
                                parkVO1 =  parkVO;
                            }
                        }
                        geConGisresource.setTotalLot(parkVO1.getTotalLot());
                        geConGisresource.setLeftLot(parkVO1.getLeftLot());
                        geConGisresource.setIntroduction("空余车位/总车位："+parkVO1.getLeftTempLot()+"/"+parkVO1.getTotalLot());
                    }
                }
            }

        }
        return geConGisresources;
    }
}
